<template>
  <div class="register-container">
    <div class="register-background">
      <div class="particles">
        <div v-for="i in 30" :key="i" class="particle" :style="particleStyle(i)"></div>
      </div>
      <div class="tech-circle"></div>
      <div class="tech-line"></div>
    </div>

    <el-card class="register-card">
      <div class="logo">
        <div class="logo-icon">
          <div class="cube">
            <div class="front"></div>
            <div class="back"></div>
            <div class="left"></div>
            <div class="right"></div>
            <div class="top"></div>
            <div class="bottom"></div>
          </div>
          <div class="scan-line"></div>
        </div>
        <span class="title">用户注册</span>
      </div>

      <el-form :model="registerForm" :rules="rules" ref="registerForm" label-position="top">
        <el-form-item label="登录名" prop="loginName">
          <el-input
              v-model="registerForm.loginName"
              placeholder="请输入登录名"
              prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input
              v-model="registerForm.name"
              placeholder="请输入姓名"
              prefix-icon="el-icon-edit"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input
              v-model="registerForm.phone"
              placeholder="请输入手机号"
              prefix-icon="el-icon-mobile-phone"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
              type="password"
              v-model="registerForm.password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
              type="password"
              v-model="registerForm.confirmPassword"
              placeholder="请再次输入密码"
              prefix-icon="el-icon-lock"
              show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
              type="primary"
              @click="handleRegister"
              class="register-btn"
          >
            注 册
          </el-button>
        </el-form-item>
        <div class="footer">
          <el-button type="text" @click="goToLogin">已有账号？立即登录</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import request from '@/utils/request'

export default {
  name: 'Register',
  data() {
    // 自定义验证规则：确认密码
    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }

    return {
      registerForm: {
        loginName: '',
        name: '',
        phone: '',
        password: '',
        confirmPassword: ''
      },
      rules: {
        loginName: [
          { required: true, message: '请输入登录名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度至少6位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: validateConfirmPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    particleStyle() {
      const size = Math.random() * 5 + 1;
      return {
        width: `${size}px`,
        height: `${size}px`,
        left: `${Math.random() * 100}%`,
        top: `${Math.random() * 100}%`,
        animationDelay: `${Math.random() * 5}s`
      };
    },
    handleRegister() {
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          // 构造注册请求数据
          const registerData = {
            loginName: this.registerForm.loginName,
            name: this.registerForm.name,
            phone: this.registerForm.phone,
            password: this.registerForm.password
          }

          request.post('/user/register', registerData)
              .then(response => {
                if (response.data.code === '200') {
                  this.$message.success('注册成功')
                  // 注册成功后跳转到登录页
                  this.$router.push('/login')
                } else if (response.data.code === '000') {
                  this.$message.error('用户名已存在')
                } else {
                  this.$message.error(response.data.msg || '注册失败')
                }
              })
              .catch(error => {
                console.error('注册失败:', error)
                this.$message.error('注册失败，请稍后重试')
              })
        }
      })
    },
    goToLogin() {
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #141e30, #243b55);
  position: relative;
  overflow: hidden;
}

.register-background {
  position: absolute;
  width: 100%;
  height: 100%;
  z-index: 0;
}

.particles {
  position: absolute;
  width: 100%;
  height: 100%;
}

.particle {
  position: absolute;
  background-color: rgba(0, 210, 255, 0.5);
  border-radius: 50%;
  animation: float 15s infinite linear;
}

.tech-circle {
  position: absolute;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  border: 2px dashed rgba(0, 210, 255, 0.2);
  right: -100px;
  bottom: -100px;
  animation: rotate 30s linear infinite;
}

.tech-line {
  position: absolute;
  width: 100%;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(0, 210, 255, 0.3), transparent);
  top: 50%;
  box-shadow: 0 0 10px rgba(0, 210, 255, 0.3);
}

.register-card {
  width: 450px;
  padding: 40px;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  z-index: 1;
}

.logo {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 30px;
}

.logo-icon {
  position: relative;
  width: 60px;
  height: 60px;
  perspective: 1000px;
  margin-bottom: 15px;
}

.cube {
  width: 100%;
  height: 100%;
  position: relative;
  transform-style: preserve-3d;
  animation: rotate 15s infinite linear;
}

.cube div {
  position: absolute;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #3a7bd5, #00d2ff);
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 0 15px rgba(58, 123, 213, 0.7);
  opacity: 0.8;
}

.front {
  transform: translateZ(30px);
}

.back {
  transform: translateZ(-30px);
}

.left {
  transform: rotateY(-90deg) translateZ(30px);
}

.right {
  transform: rotateY(90deg) translateZ(30px);
}

.top {
  transform: rotateX(90deg) translateZ(30px);
}

.bottom {
  transform: rotateX(-90deg) translateZ(30px);
}

.scan-line {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.8), transparent);
  animation: scan 3s infinite linear;
  box-shadow: 0 0 15px #fff;
  z-index: 2;
}

.title {
  font-size: 24px;
  font-weight: 600;
  letter-spacing: 2px;
  color: #fff;
  text-shadow: 0 0 10px rgba(0, 210, 255, 0.7);
}

/deep/ .el-form-item__label {
  color: #fff !important;
  font-weight: 400;
}

/deep/ .el-input__inner {
  background: rgba(255, 255, 255, 0.1) !important;
  border: 1px solid rgba(255, 255, 255, 0.2) !important;
  color: #fff !important;
  height: 48px;
  border-radius: 6px;
}

/deep/ .el-input__inner::placeholder {
  color: rgba(255, 255, 255, 0.5) !important;
}

/deep/ .el-input__prefix {
  color: rgba(255, 255, 255, 0.7) !important;
}

.register-btn {
  width: 100%;
  height: 48px;
  background: linear-gradient(90deg, #3a7bd5, #00d2ff) !important;
  border: none !important;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 2px;
  margin-top: 10px;
  transition: all 0.3s;
}

.register-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(58, 123, 213, 0.4);
}

.footer {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

/deep/ .el-button--text {
  color: rgba(255, 255, 255, 0.7) !important;
}

/deep/ .el-button--text:hover {
  color: #00d2ff !important;
}

@keyframes rotate {
  0% {
    transform: rotateX(0) rotateY(0);
  }
  100% {
    transform: rotateX(360deg) rotateY(360deg);
  }
}

@keyframes scan {
  0% {
    top: -10px;
    opacity: 0;
  }
  10% {
    opacity: 1;
  }
  90% {
    opacity: 1;
  }
  100% {
    top: 100%;
    opacity: 0;
  }
}

@keyframes float {
  0% {
    transform: translateY(0) translateX(0);
  }
  50% {
    transform: translateY(-100px) translateX(20px);
  }
  100% {
    transform: translateY(0) translateX(0);
  }
}
</style>